<?php
/**
 * Simple Model Test Script
 * Tests Model table mappings without full webman bootstrap
 */

// Load environment variables from .env file
$envFile = __DIR__ . '/.env';
if (file_exists($envFile)) {
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        if (strpos($line, '=') !== false && strpos($line, '#') !== 0) {
            list($key, $value) = explode('=', $line, 2);
            $_ENV[trim($key)] = trim($value);
        }
    }
}

// Simple autoloader for app classes
spl_autoload_register(function ($class) {
    // Convert namespace to file path
    $file = str_replace('\\', '/', $class) . '.php';
    $fullPath = __DIR__ . '/' . $file;
    
    if (file_exists($fullPath)) {
        require_once $fullPath;
        return true;
    }
    
    return false;
});

echo "🧪 Testing Model Table Mappings...\n\n";

// Models to test
$models = [
    'app\common\model\Device' => 'gf_device',
    'app\common\model\ChannelStats' => 'gf_channel_stats',
    'app\common\model\AttributionEvent' => 'gf_attribution_event',
    'app\common\model\AttributionInstall' => 'gf_attribution_install',
    'app\common\model\AttributionTrack' => 'gf_attribution_track',
];

$successCount = 0;
$failedCount = 0;
$errors = [];

foreach ($models as $modelClass => $expectedTable) {
    echo "Testing: $modelClass\n";
    echo "Expected table: $expectedTable\n";
    
    try {
        // Check if class exists
        if (!class_exists($modelClass)) {
            echo "  ❌ Class not found: $modelClass\n";
            $failedCount++;
            $errors[] = "$modelClass: Class not found";
            continue;
        }
        
        // Get table name from model
        try {
            $reflection = new ReflectionClass($modelClass);
            if ($reflection->hasProperty('table')) {
                $property = $reflection->getProperty('table');
                $property->setAccessible(true);
                
                // Create instance to get table name
                $instance = $reflection->newInstance();
                $actualTable = $property->getValue($instance);
                
                echo "  Actual table: $actualTable\n";
                
                if ($actualTable === $expectedTable) {
                    echo "  ✅ Table name matches expected value\n";
                    $successCount++;
                } else {
                    echo "  ❌ Table name mismatch: expected $expectedTable, got $actualTable\n";
                    $failedCount++;
                    $errors[] = "$modelClass: Table name mismatch";
                }
            } else {
                echo "  ❌ No 'table' property found\n";
                $failedCount++;
                $errors[] = "$modelClass: No table property";
            }
        } catch (Exception $e) {
            echo "  ❌ Could not get table name: " . $e->getMessage() . "\n";
            $failedCount++;
            $errors[] = "$modelClass: " . $e->getMessage();
        }
        
    } catch (Exception $e) {
        echo "  ❌ Unexpected error: " . $e->getMessage() . "\n";
        $failedCount++;
        $errors[] = "$modelClass: Unexpected error - " . $e->getMessage();
    }
    
    echo "\n";
}

echo "📊 TEST RESULTS:\n";
echo "==================================================\n";
echo "Total models tested: " . count($models) . "\n";
echo "✅ Successful: $successCount\n";
echo "❌ Failed: $failedCount\n";
echo "Success rate: " . round(($successCount / count($models)) * 100, 2) . "%\n\n";

if ($failedCount > 0) {
    echo "❌ ERRORS ENCOUNTERED:\n";
    echo "==================================================\n";
    foreach ($errors as $error) {
        echo "- $error\n";
    }
    echo "\n";
}

if ($successCount === count($models)) {
    echo "🎉 ALL MODEL TABLE MAPPINGS ARE CORRECT!\n";
    echo "✅ All models use the expected gf_ prefixed table names.\n";
} else {
    echo "⚠️  Some issues were found. Please review the errors above.\n";
}

echo "\n🏁 Model table mapping test completed!\n";

?>